package myserver;
import dao.Dao;
import java.io.*;
import java.net.*;

/**
 * 用来监听用户的注册信息，并写入文件
 */
public class RegisterServer extends Thread{
	public void run(){
//		System.out.println("register");
		ServerFrame.area.append("regist\n");
		whileWait();
	}
	
	/**
	 * 	循环等待用户的注册信息
	 */
	 private void whileWait(){
		try {
			while(true){
				ServerSocket ss = new ServerSocket(8999);
				Socket s = ss.accept();
//				System.out.println("已经接受连接");
				ServerFrame.area.append("已经接受连接");
				BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
				String sName = input.readLine();
				String pd = input.readLine();
				int sex = Integer.parseInt(input.readLine());
				String id = Dao.addUser(sName, pd, sex);
//				System.out.println(sName+"-->"+pd+"-->"+sex);
				ServerFrame.area.append(sName+"-->"+pd+"-->"+sex+"\n");
				PrintWriter writerClient = new PrintWriter(s.getOutputStream());
//				writerClient.println("您已经注册成功！");
				writerClient.println(id);
				writerClient.close();
				input.close();
				s.close();
				ss.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
//	public static void main(String[] args) {
//		new RegisterServer().whileWait();
//	}
}
